package leetcode.biweekly.week80;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

//Solution3Test
public class Solution3 {

	// Solution3Test
	public boolean matchReplacement(String s, String sub, char[][] mappings) {
		char[] ss = s.toCharArray();
		char[] sb = sub.toCharArray();
		Map<Character, Set<Character>> hash = new HashMap<>();
		for (int i = 0; i < mappings.length; i++) {
			char[] cur = mappings[i];
			char from = cur[0], to = cur[1];
			Set<Character> set = hash.getOrDefault(from, new HashSet<>());
			set.add(to);
			hash.put(from, set);
		}
		int len1 = ss.length;// "fool3e7bar" 10
		int len2 = sb.length;// "leet" 4
		for (int i = 0; i <= len1 - len2; i++) {
			char c = ss[i];
			int j = 0;
			while (j < len2 && (c == sb[j] || (hash.containsKey(sb[j]) && hash.get(sb[j]).contains(c)))) {
				j++;
				if (i + j < len1)
					c = ss[i + j];
			}
			if (j == len2) {
				return true;
			}
		}
		return false;
	}

}
